<?php
include('student_lock.php');

$semester = $_GET["sem"] ? $_GET["sem"] : "fall";
$year = $_GET["yr"]  ? $_GET["yr"]  : "2010";

$sql1="SELECT * 
FROM course_taken
JOIN course ON course_taken.course_id = course.id
JOIN section ON course_taken.course_id = section.course_id
AND course_taken.section_number = section.section_number
JOIN time_slot ON time_slot.id = section.time_slot
WHERE course_taken.student_id = 'mm' AND course.semester = '".$semester."' AND course.year = '".$year."'";

//****get student's name********
	$login = $_SESSION["login_user"];
	$result=mysql_query("select first from student where id='$login'");
	$row=mysql_fetch_array($result);
	$name = $row['first'];
//******************************
?>
<head>
<script src="jquery.js" type="text/javascript"></script> 
<script TYPE="TEXT/JAVASCRIPT">
<?php
	echo "var login = '$login';"
	
?>

function semesterSelectorHandler(input)
{
	a = input.split('_');
	location.href="student.php?sem="+a[0]+"&yr="+a[1];
	
	
}

function dropClass(classId, sectionId)
{
	$.ajax({url: 'dropTable.php?course='+classId+'&section='+sectionId+'&user='+login});
	location.reload(true);
}
</script>
</head>
<body>
<center>
<table align="center">
<tr><td width="200">Welcome <?php echo $_SESSION['login_user']; ?></td>
<td width="400" align="center"><h1>Student Schedule Page</h1></td>
<td width="200" align="right"><a href="logout.php">Logout</a></td>
</tr>
</table>
<br/>
<table width="800">
	<tr>
		<td width="200"><?php echo "Hello ".$name;?></td>
		<td width="400"></td>
		<td align="right">Previous Semesters</td>
</tr>
<tr>
		<td>Welcome to Scheduletron!</td>
		<td></td>
		<td align="right">
		<?php
			// select distinct semester/year combintions
			$sql2 = "SELECT semester, year FROM course GROUP BY semester, year";
			$result = mysql_query($sql2);
			echo "<form action='' name='selecterForm'>";
			echo "<select name='semesterSelector' onchange=\"semesterSelectorHandler(document.selecterForm.semesterSelector.value);\">";
			echo "<option>Select Semester</option>";
			while ($row = mysql_fetch_array($result))
			{
				echo "<option value='".$row['semester']."_".$row['year']."'>".$row['semester']." ".$row['year']."</option>";
			}
			echo "</select>";
		?>
		</td>
</tr>
<tr>
		<td width="200"></td>
		<td width="400"></td>
		<td align="right"><?php echo "Selected: ".$semester." ".$year;?></td>
</tr>
</table>

<table border="1" width="800">
<tr>
		<td width="100">Class Number</td>
		<td width="50">Section</td>
		<td width="250">Class Name</td>
		<td width="120">Days/Time</td>
		<td width="120">Instructor Name</td>
		<td width="40">Room</td>
		<td width="20">&nbsp</td>
		<td>Drop</td>
</tr>

<!-- add loop to output rows -->
<?php
$result=mysql_query($sql1);
while ($classes=mysql_fetch_array($result))
{
	echo "<tr>";
	echo "<td>".$classes['number']."</td>";
	echo "<td>".$classes['section_number']."</td>";
	echo "<td>".$classes['name']."</td>";
	echo "<td>".$classes['days']." ".$classes['start'].":".$classes['end']."</td>";
	echo "<td>".$classes['instructor']."</td>";
	echo "<td>".$classes['building_short_name']."-".$classes['room_number']."</td>";
	echo "<td>".$classes['grade']."</td>";
	$class_id = $classes['course_id'];
	$section_id = $classes['section_number'];
	echo "<td><input type='Button' value='Drop' onClick=\"dropClass('".$class_id."', '".$section_id."')\"></td>";
	echo "</tr>";
}
?>
<!-- add loop to output rows -->
</table>
</br>
<table width="800">
	<tr>
		<td width="200">
			<input type="Button" value="Add New Class" onClick="location.href='student_add_class.php'">
		</td>
		<td></td>
		<td width="200" align="right"><input type="Button" value="View profile" onClick="location.href='view_profile.php'"></td>
</tr>
</table>
</center>
</body>
